// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CustomLogLevelTraceSwitchProvider.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   This <see cref="ITraceSwitchProvider"/> will set the log level to a custom value for all <see cref="ITrace"/>.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using AzureContrib.WindowsAzure.Properties;

namespace AzureContrib.WindowsAzure.Diagnostics
{
	/// <summary>
	/// This <see cref="ITraceSwitchProvider"/> will set the log level to a custom value for all <see cref="ITrace"/>.
	/// </summary>
	public class CustomLogLevelTraceSwitchProvider : ITraceSwitchProvider
	{
		private TraceSwitch loosyGoosyTraceSwitch;

		/// <summary>
		/// Initializes a new instance of the <see cref="CustomLogLevelTraceSwitchProvider"/> class.
		/// </summary>
		/// <param name="logLevel">The <see cref="LogLevel"/> for all traces.</param>
		/// <remarks>>This Version of <see cref="ITraceSwitchProvider"/> will trace everything!</remarks>
		public CustomLogLevelTraceSwitchProvider(LogLevel logLevel)
		{
			loosyGoosyTraceSwitch = new TraceSwitch("*", Strings.VerboseTraceSwitchDescription, logLevel);
		}

		/// <summary>
		/// Find the <see cref="TraceSwitch"/> for this <see cref="ITraceSwitchable"/>?
		/// </summary>
		/// <param name="traceSwitchable">The <see cref="ITraceSwitchable"/> to find a <see cref="TraceSwitch"/> for.</param>
		/// <returns>A <see cref="TraceSwitch"/> that matches the <paramref name="traceSwitchable"/>.</returns>
		/// <remarks>This implementation returns the same very verbose <see cref="TraceSwitch"/> for all requests.</remarks>
		public TraceSwitch GetSwitch(ITraceSwitchable traceSwitchable)
		{
			return loosyGoosyTraceSwitch;
		}

		/// <summary>
		/// Check if logging is enabled for <paramref name="traceSwitchable"/> on the <paramref name="logLevel"/>.
		/// </summary>
		/// <param name="logLevel">The level to control logging for.</param>
		/// <param name="traceSwitchable">The <see cref="ITraceSwitchable"/> to check for enablement.</param>
		/// <returns>True if logging is enabled.</returns>
		public bool Enabled(LogLevel logLevel, ITraceSwitchable traceSwitchable)
		{
			return logLevel <= loosyGoosyTraceSwitch.Level;
		}
	}
}